/*
* 返回顶部滚动插件
* author Peter.zheng
* ver 1.0
* 2011-9-17
* 使用方法：$("body").topWidgets();
* */
;(function($){
		       $.fn.topWidgets=function(options){
			       var options=$.extend({
                        alignObj:"body",
                        distanceBottom:0,
                        duration:"fast",
                        scrollTo:0,
                        offsetX:0,
                        bottom:0,
                        topClass:"app_scroll_bar"
				   },options||{});
				   return this.each(function(){
    							var clientHeight =0;
    							var scrollHeight=Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    							var de=document.documentElement;  
                                var winHeight=window.innerHeight||(de&&de.clientHeight)||document.body.clientHeight; 
                                //页面高度==视口高度，无需要top按纽
    							if(scrollHeight==winHeight){
    							    return false;
    						 }
				            /*create dom*/
					         var $_topBtn=$("."+options.topClass);
							 if($_topBtn.size()==0){
							    $_topBtn=$('<a class="'+options.topClass+' seo" title="返回顶部" href="javascript:void(0)" ></a>');
                                $_topBtn.appendTo($("body"));
							 }
							//设置btn的位置
							if(options.alignObj=="body"){
							    var _topBtn_x=($(window).width()-$(options.alignObj).width())*0.5+$(options.alignObj).width()-$_topBtn.outerWidth()+options.offsetX;
							}else{
                                var _topBtn_x=($(window).width()-$(options.alignObj).width())*0.5+$(options.alignObj).width()+options.offsetX;
							}
							 $_topBtn.css("left",_topBtn_x);
							 /*判断是否滚动到自定义的位置*/
							var scrollTop=0;
							var isIE6=false;
							if($.browser.msie&&parseInt($.browser.version)==6){
								   isIE6=true;
								}
                           //流动时添加判断
                           $(window).bind("scroll",function(){
		                        	   
		                        	   if (document.documentElement && document.documentElement.scrollTop) {
		                        	        scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
		                        	    } else if (document.body) {
		                        	        scrollTop = document.body.scrollTop;
		                        	    }
		                        	   if(document.body.clientHeight&&document.documentElement.clientHeight){
		    						       clientHeight=(document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
		    						   }else{
		    						       clientHeight=(document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
		    						   }
		    					   scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
		    					   /*ie6进行特殊处理*/
		    					   if(!isIE6){
		   							    var _topBtn_bottom=options.bottom;
		   							    $_topBtn.css({"bottom":_topBtn_bottom,"position":"fixed"});
		   							}else{
			   							    
		                                    var ie6_bottom=clientHeight+scrollTop-options.bottom-$_topBtn.outerHeight();
		                                    $_topBtn.css("top",ie6_bottom);
		   								}
	                        	   if((scrollTop+clientHeight-options.distanceBottom)<=(scrollHeight-options.distanceBottom)&&scrollTop!=0){
	                            	         $_topBtn.show();
	                                   }else{
	                                         $_topBtn.fadeOut();
	                                 }; 
                               });
                           //点击返回顶部
                           $_topBtn.bind("click",function(){
                        	      var $_winObj=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body');
									if(!$_winObj.is(":animated")){
										   $_winObj.animate({scrollTop:0}, 'slow');
									};
                        	      return false;  
                               });
                               
                            $(window).resize(function() {
                        	   
                        	   	if(options.alignObj=="body"){
										var _topBtn_x=($(window).width()-$(options.alignObj).width())*0.5+$(options.alignObj).width()-$_topBtn.outerWidth()+options.offsetX;
								}else{
										var _topBtn_x=($(window).width()-$(options.alignObj).width())*0.5+$(options.alignObj).width()+options.offsetX;
								}
							    $_topBtn.css("left",_topBtn_x);
                           });   
    
                              
                                    
				   })
			   } 
		 })(jQuery);